{include file="common/header" /}
<link rel="stylesheet" href="__LAYUIMINI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<style type="text/css">
    .orderidclass{
        width:40px;text-align:center;margin-right:3px;height:18px;
    }
</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input" >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户性别</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sex" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户城市</label>
                            <div class="layui-input-inline">
                                <input type="text" name="city" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户职业</label>
                            <div class="layui-input-inline">
                                <input type="text" name="classify" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit  lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset> -->


        <div class="layuimini-container">
		    <div class="layuimini-main">
		        
		        <div>
		            <div class="layui-btn-group">
		                <a href="{:url('System/right_add')}"><button class="layui-btn" >添加权限</button></a>
		                <!-- <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button> -->
		            </div>
		        </div>
		    </div>
		</div>


        <table class="layui-table layui-form" lay-size="sm">
		  <colgroup>
            <col width="320">
            <col width="200">
            <col >
		    <!-- <col width="300"> -->
		    <col width="120">
            <col width="100">
            <col width="300">
		  </colgroup>
		  <thead>
		    <tr>
		      <th >名称</th>
		      <th>节点跳转</th>
              <th>权限码</th>
		      <th>是否显示</th>
              <th>排序</th>
		      <th>操作</th>
		    </tr> 
		  </thead>
		  <tbody>

		  	{foreach name="$right_list" item="vo" key="k" }
		  	<tr>
		      	<td>{$vo.str | raw} <i class="fa {$vo.icon}"></i>  {$vo.name}</td>
			    <td>{$vo.href}</td>
                <td>{$vo.right}</td>
			    
                <td >
                    <input type="checkbox" data-table="system_menu" data-id_name="id" data-id="{$vo.id}" data-field="show" lay-skin="switch" lay-text="显示|不显示" lay-filter="switchIsOpen" {if $vo.show == 1 /} checked {/if}>
                </td>
                <td >
                    <input type="number" name="" onBlur="checkSort(this)" data-table="system_menu" data-id_name="id" data-id="{$vo.id}" data-field="sort" value="{$vo.sort}" class="orderidclass">
                </td>
			    <td>
					<div >
                        <a class="layui-btn layui-btn-xs " href="{:url('System/right_add',array('pid'=>$vo['id']))}">在此栏目下添加</a>

                        <a class="layui-btn layui-btn-xs " href="{:url('System/right_edit',array('id'=>$vo['id']))}">编辑</a>
                        <a class="layui-btn layui-btn-xs layui-btn-danger"  href="javascript:void(0)" data-url="{:url('System/right_del')}" data-id="{$vo.id}" onClick="delfun(this)">删除</a>

					</div>
				</td>
		    </tr>
		  	{/foreach}
		  </tbody>
		</table>
    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layuimini = layui.layuimini;

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: { curr: 1 }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        //是否开启
        form.on('switch(switchIsOpen)', function(data){
            var checked = data.elem.checked;
            //var role_id = data.elem.value;
            let table = $(data.elem).attr("data-table");
            let id_name = $(data.elem).attr("data-id_name");
            let id = $(data.elem).attr("data-id");
            let field = $(data.elem).attr("data-field");
            console.log(table)
            // var id = data.elem.attributes['switch_id'].nodeValue;
            // var pri_id = data.elem.attributes['switch_pri_id'].nodeValue;
            // var tempId = data.elem.attributes['switch_tempId'].nodeValue;

            $.ajax({
                url:"{:url(CONTROLLER_NAME.'/changeStatus')}",
                data: {'table':table,'checked':checked,'id_name':id_name,'id':id,'field':field},
                type:'post',
                dataType:'json',
                success:function(res){
                    // console.log(res);
                    // return false;
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
        });

        //改变排序
        window.checkSort=function(obj){
            let table = $(obj).attr("data-table");
            let id_name = $(obj).attr("data-id_name");
            let id = $(obj).attr("data-id");
            let val = $(obj).val();
            let field = $(obj).attr("data-field");

            $.ajax({
                url:"{:url(CONTROLLER_NAME.'/changeOrderid')}",
                data: {'table':table,'id_name':id_name,'id':id,'field':field,'val':val},
                type:'post',
                dataType:'json',
                success:function(res){
                    // console.log(res);
                    // return false;
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
        };

        // 监听添加操作
        $(".data-add-btn").on("click", function () {
            layerOpenWindow("{:url('admin/add')}","添加用户",['80%', '80%'],true);
            return false;
        });

        window.delfun = function(obj){
            // 删除按钮
	        layer.confirm('确认删除？', {
				title:'温馨提示',closeBtn:0,icon:3,
	            btn: ['确定', '取消'] //按钮
	        }, function () {
	            $.ajax({
	                type: 'post',
	                url: $(obj).attr('data-url'),
	                data: {del_id: $(obj).attr('data-id')},
	                dataType: 'json',
	                success: function (data) {
	                    layer.closeAll();
	                    if (data.errcode == 1) {
	                        $(obj).parent().parent().parent().remove();
	                    } else {
	                        layer.alert(data, {icon: 2});  //alert('删除失败');
	                    }
	                }
	            })
	        }, function () {
	            layer.closeAll();
	        });
        };

        // 监听删除操作
        /*$(".data-delete-btn").on("click", function () {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;

            var ids = '';
            if (data.length == 0) {
                layer.msg("请选择数据",{icon:2})
                return false
            } else {
                for (var i = 0; i < data.length; i++) {
                    ids += data[i].admin_id + ',';
                }
                ids=ids.substring(0,ids.length-1);
            }

            $.ajax({
                url:"{:url('admin/deleteMore')}",
                data: {ids:ids},
                type:'post',
                dataType:'json',
                success:function(res){
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
            // layer.alert(JSON.stringify(data));
        });*/

        //监听表格复选框选择
        /*table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });*/

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                layerOpenWindow("{:url('admin/edit')}?admin_id="+data.admin_id,"修改用户",['80%', '80%'],true);

                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除吗', function (index) {
                    
                    $.ajax({
                        url:"{:url('admin/delete')}",
                        data: {id:data.admin_id},
                        type:'post',
                        dataType:'json',
                        success:function(res){
                            if(res.errcode == 1){
                                layer.msg(res.message,{icon: 1,time: 1000},function(){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    location.reload();
                                })
                            }else {
                                layer.msg(res.message,{icon: 2,time: 2000})
                            }
                            // parent.layer.close(index);
                        },
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                            layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                        }
                    });

                });
            }
        });

    });
</script>

</body>
</html>